/** */
package org.tiger.springboot.redis.jedis;

import com.google.common.base.Splitter;
import com.google.common.collect.Sets;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.tiger.common.constant.CharacterConstant;
import org.tiger.springboot.redis.domain.RedisPropertyBean;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.util.List;
import java.util.Set;

/** @author liuwenlai */
@Configuration
public class JedisClusterConfig {

  @Autowired private RedisPropertyBean redisPropertyBean;

  // TODO wait to test
  public JedisCluster getJedisCluster() {

    // 集群的所有端口ip
    List<String> serverPorts =
        Splitter.on(CharacterConstant.CHARACTER_COMMA)
            .splitToList(redisPropertyBean.getClusterNodes());
    Set<HostAndPort> hostAndPorts = Sets.newHashSet();

    for (int i = 0; i < serverPorts.size(); i++) {
      List<String> serverAndPort =
          Splitter.on(CharacterConstant.CHARACTER_COLON).splitToList(serverPorts.get(i));
      hostAndPorts.add(
          new HostAndPort(serverAndPort.get(0), Integer.valueOf(serverAndPort.get(1))));
    }

    // 返回集群句柄
    return new JedisCluster(hostAndPorts, redisPropertyBean.getCommandTimeout());
  }

  public JedisCluster getJedisClusterWithConfig() {

    // 集群的所有端口ip
    List<String> serverPorts =
        Splitter.on(CharacterConstant.CHARACTER_COMMA)
            .splitToList(redisPropertyBean.getClusterNodes());
    Set<HostAndPort> hostAndPorts = Sets.newHashSet();

    for (int i = 0; i < serverPorts.size(); i++) {
      List<String> serverAndPort =
          Splitter.on(CharacterConstant.CHARACTER_COLON).splitToList(serverPorts.get(i));
      hostAndPorts.add(
          new HostAndPort(serverAndPort.get(0), Integer.valueOf(serverAndPort.get(1))));
    }

    // 返回集群句柄
    return new JedisCluster(hostAndPorts, getConfig());
  }

  @Bean(name = "stringRedisTemplateByJedisConfig")
  public StringRedisTemplate stringRedisTemplate() {
    StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(getJedisConnectionFactory());

    return stringRedisTemplate;
  }

  public RedisTemplate<String, ?> redisTemplate() {
    RedisTemplate<String, ?> redisTemplate = new RedisTemplate<>();

    return redisTemplate;
  }

  public JedisConnectionFactory getJedisConnectionFactory() {
    JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
    String[] ipAndPort = redisPropertyBean.getSingleNode().split(CharacterConstant.CHARACTER_COLON);
    connectionFactory.setHostName(ipAndPort[0]);
    connectionFactory.setPort(Integer.valueOf(ipAndPort[1]));
    connectionFactory.setPoolConfig(getConfig());
    return connectionFactory;
  }

  public JedisPoolConfig getConfig() {

    JedisPoolConfig config = new JedisPoolConfig();
    config.setMaxTotal(redisPropertyBean.getMaxTotal());
    config.setMaxIdle(redisPropertyBean.getMaxIdle());
    config.setMaxWaitMillis(redisPropertyBean.getMaxWaitMillis());
    config.setMinIdle(redisPropertyBean.getMinIdle());
    config.setTestOnBorrow(redisPropertyBean.isTestOnBorrow());
    config.setTestOnReturn(redisPropertyBean.isTestWhileIdle());

    return config;
  }
}
